﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Reflection;

namespace Test
{
    static class testStackTrace
    {
        public static void parentMethod()
        {
            var t = new MyTest();
            t.add(1);

            // 特性构造方法中测试结果 System.Reflection.CustomAttribute
            typeof(MyTest).GetCustomAttribute<testAttribute>();


        }
    }

    [test("dsfdsf", "dsfsdf")]
    public class MyTest
    {
        public MyTest([System.Runtime.CompilerServices.CallerMemberName]string member = null)
        {
            var ss = new StackTrace(true);
            var t = ss.GetFrame(1).GetMethod().DeclaringType;

            Console.WriteLine("构造方法中：{0}，CallerMemberName：{1}", t.FullName, member);
        }

        public void add(int i, [System.Runtime.CompilerServices.CallerMemberName]string member = null)
        {
            i++;

            var ss = new StackTrace(true);
            var t = ss.GetFrame(1).GetMethod().DeclaringType;

            Console.WriteLine("一般方法中：{0}，CallerMemberName：{1}", t.FullName, member);
        }
    }

    public class testAttribute : Attribute
    {
        public string Name { get; private set; }

        public string Description { get; private set; }

        public testAttribute(string name, string desc, [System.Runtime.CompilerServices.CallerMemberName]string member = null)
        {
            Name = name;
            Description = desc;

            var ss = new StackTrace(true);
            var t = ss.GetFrame(1).GetMethod().DeclaringType;

            Console.WriteLine("特性构造方法中：{0}，CallerMemberName：{1}", t.FullName, member);
        }
    }
}
